package com.ethan.h2.controller;

import com.ethan.h2.dao.UserDao;
import com.ethan.h2.dto.UserAddDTO;
import com.ethan.h2.dto.UserEditDTO;
import com.ethan.h2.dto.UserIdDTO;
import com.ethan.h2.po.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ethan.liu
 * @Date: 2020/10/16 21:59
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserDao userDao;
    @PostMapping("add")
    public User add(@RequestBody UserAddDTO userAddDTO){
        User user = new User();
        BeanUtils.copyProperties(userAddDTO,user);
        return userDao.save(user);
    }
    @PostMapping("edit")
    public User edit(@RequestBody UserEditDTO userAddDTO){
        User user = new User();
        BeanUtils.copyProperties(userAddDTO,user);
        return userDao.saveAndFlush(user);
    }
    @PostMapping("del")
    public String del(@RequestBody UserIdDTO userIdDTO){
        userDao.deleteById(userIdDTO.getId());
        return "success";
    }
    @PostMapping("get")
    public User get(@RequestBody UserIdDTO userIdDTO){
        return userDao.getOne(userIdDTO.getId());
    }
    @GetMapping("get_all")
    public List<User> getAll(){
        return userDao.findAll();
    }
}
